import {defineStore} from 'pinia'
import {CreatedMenuData, getRegisterRouter} from "@/apps/admin/units/MenuTool";
import cache from "@/units/cache"

type stateType = {
    menu: Array<any>
}
/**
 * 后台菜单
 */
export default defineStore('app-menu', {
    state(): stateType {
        return {
            menu: []
        }
    },
    actions: {
        /**
         * 储存菜单数据
         * @param menu
         */
        setMenuData(menu: Array<any>) {
            // @ts-ignore
            this.menu = menu
        },


        /**
         * 路由信息
         * @param path
         */
        getRouterInfo(path: string): any {
            for (let router of this.routerData) {
                if (router.path === path) {
                    return router
                }
            }
            return null
        },

        /**
         * 清空菜单值
         */
        clear() {
            this.menu = []
        }
    },


    getters: {
        /**
         * 布局左侧菜单数据
         */
        menuData(): Array<any> {
            return CreatedMenuData(this.menu)
        },

        /**
         * 需要注册的路由
         */
        routerData(): Array<any> {
            return getRegisterRouter(this.menu);
        }
    },

    persist: {//持久化储存
        enabled: true,
        strategies: [
            {
                key: `${cache.prefix()}-store-menu`, // 默认key是上面store的id，可自定义key
                storage: localStorage,//本地储存驱动
                paths: ['menu']
            }
        ]
    }
})